<template>
  <div>
    <nav-header></nav-header>
    <nav-bread>
      <span>商品列表页面</span>
    </nav-bread>
    <div class="accessory-result-page accessory-page">
      <div class="container">
        <div class="filter-nav">
          <span class="sortby">Sort by:</span>
          <a href="javascript:void(0)" class="default cur">Default</a>
          <a @click="sortGoods" href="javascript:void(0)" class="price">Price
            <svg class="icon icon-arrow-short">
              <use xlink:href="#icon-arrow-short"></use>
            </svg>
          </a>
          <a href="javascript:void(0)" class="filterby stopPop">Filter by</a>
        </div>
        <div class="accessory-result">
          <!-- filter -->
          <div class="filter stopPop" id="filter">
            <dl class="filter-price">
              <dt>Price:</dt>
              <dd><a href="javascript:void(0)">All</a></dd>
              <dd>
                <a href="javascript:void(0)">0 - 100</a>
              </dd>
            </dl>
          </div>

          <!-- search result accessories list -->
          <div class="accessory-list-wrap">
            <div class="accessory-list col-4">
              <ul>
                <li v-for="item in goodsList">
                  <div class="pic">
                    <a href="#"><img :src="'/static/img/'+item.productImage" alt=""></a>
                  </div>
                  <div class="main">
                    <div class="name">{{item.productName}}</div>
                    <div class="price">￥{{item.salePrice}}</div>
                    <div class="btn-area">
                      <a href="javascript:;" class="btn btn--m" @click="addCart(item.productId)">加入购物车</a>
                    </div>
                  </div>
                </li>
              </ul>
              <div v-infinite-scroll="loadMore" infinite-scroll-disabled="busy" infinite-scroll-distance="10">
                加载中...
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <modal :mdShow="mdShow" @close="closeModal">
      <p slot="message">{{msg}}</p>
      <div slot="btnGroup">
        <a href="#" class="btn btn--m" @click="closeModal">关闭</a>
      </div>
    </modal>

    <modal :mdShow="mdShowCart" @close="mdShowCart=false">
      <p slot="message">{{msg}}</p>
      <div slot="btnGroup">
        <a href="#" class="btn btn--m" @click="mdShowCart=false">继续购物</a>
        <router-link class="btn btn--m" to="/cart">去购物车结算</router-link>
      </div>
    </modal>

    <nav-footer></nav-footer>
  </div>
</template>

<script>
  import '../assets/css/base.css'
  import '../assets/css/login.css'
  import '../assets/css/product.css'
  import {getCookie} from '../assets/js/cookie-util'
  import NavHeader from '../components/Header'
  import NavFooter from '../components/Footer'
  import NavBread from '../components/Bread'
  import axios from 'axios'
  import Modal from '../components/Modal'

  export default {
    name: "GoodsList",
    components: {
      NavHeader, NavFooter, NavBread, Modal
    },
    data() {
      return {
        goodsList: [],
        sortFlag: true,
        page: 1,
        pageSize: 8,
        priceLevel: "all",
        busy: true,  //默认禁止加载
        mdShow: false,
        msg: "",
        mdShowCart: false,
      }
    },
    methods: {
      checkLogin(){
        if(!getCookie("token")){
          this.$store.commit("updateNickName",'')
          this.$store.commit("setCartCount",0)
          window.localStorage.clear()
          return
        }

        axios.get("/users/checkLogin").then(res=>{
          if(res.data.status==0){
            this.$store.commit('updateNickName', res.data.result.nickName)
            this.$store.commit('setCartCount', res.data.result.cartCount)
          }else{
            alert(res.data.msg)
          }
        }).catch(err=>{
          alert(err.msg)
        })
      },
      getGoodsList(flag) {
        let param = {
          page: this.page,
          pageSize: this.pageSize,
          sort: this.sortFlag ? 1 : -1,
          priceLevel: this.priceLevel
        };
        axios.get("/goods/list", {
          params: param
        }).then(res => {
          if (res.data.status == 0) {
            if (flag) {
              this.goodsList = this.goodsList.concat(res.data.result.list);
              if (res.data.result.count == 0 || res.data.result.count < this.pageSize) {
                this.busy = true;
              } else {
                this.busy = false;
              }
            } else {
              this.goodsList = res.data.result.list
              this.busy = false;
            }
          } else {
            alert(res.data.msg)
          }
        }).catch(res => {
          alert(res.message)
        })
      },
      sortGoods() {
        this.sortFlag = !this.sortFlag;
        this.page = 1;
        this.getGoodsList();
      },
      loadMore() {
        this.busy = true;   //禁止它再去滚动加载
        setTimeout(() => {
          this.page++;
          this.getGoodsList(true);
        }, 1000);
      },

      //加入购物车
      addCart(productId) {
        axios.post("/goods/addCart", {
          productId: productId
        }).then(res => {
          if (res.data.status == 0) {
            if(res.data.result=="not exists"){
              this.$store.commit('increaseCartCount')
            }
            this.msg = "加入购物车成功"
            this.mdShowCart = true
          } else {
            this.msg = res.data.msg
            this.mdShow = true
          }
        }).catch(res => {
          this.msg = res.response.data.msg
          this.mdShow = true
        })
      },
      closeModal() {
        this.mdShow = false
      }
    },
    mounted() {
      this.$nextTick(() => {
        this.checkLogin();
        this.getGoodsList();
      })
    }
  }
</script>

<style scoped>

</style>
